<template id="user">
  <div align="center">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="输入关键字">
        <el-input v-model="keyWord" placeholder="想要搜索的内容"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="selConditionLandlord()"
          >查询</el-button
        >
      </el-form-item>
    </el-form>
    <div style="width: 55%; margin-left: 630px">
      <el-button type="danger" plain @click="recoverUser()">批量恢复</el-button>
      <el-button type="danger" plain @click="delAll()">批量删除</el-button>
    </div>

    <el-table
      ref="multipleTable"
      :data="userList.list"
      tooltip-effect="dark"
      style="width: 80%"
      align="center"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" prop="user_id">
      </el-table-column>

      <el-table-column label="头像" align="center">
        <template slot-scope="scope">
            <el-image
              style="width: 100px; height: 100px"
              :src="scope.row.user_photo"
            >
            </el-image>
        </template> 
      </el-table-column>

      <el-table-column prop="user_name" label="真实姓名" width="120">
      </el-table-column>
      <el-table-column
        prop="user_sex"
        label="性别"
        width="120"
      ></el-table-column>
      <el-table-column
        prop="user_telephone"
        label="电话号码"
        width="120"
      ></el-table-column>
      <el-table-column
        prop="user_password"
        label="用户密码"
        width="120"
      ></el-table-column>
      <el-table-column
        prop="user_state"
        label="状态"
        width="120"
      ></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="danger"
            @click="recoverOneUser(scope.row.user_id)"
            >恢复</el-button
          >

          <el-button
            size="mini"
            type="danger"
            @click="delOneUser(scope.row.user_id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="userList.pageNum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="5"
      layout="total, sizes, prev, pager, next, jumper"
      :total="userList.total"
    >
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      userList: {},
      currentPage: 1,
      pageSize: 5,
      ids: [],
      keyWord: "",
    };
  },

  methods: {
    localData() {
      this.$axios
        .get("/selAllLandlord", {
          params: {
            currentPage: this.currentPage,
            pageSize: this.pageSize,
          },
        })
        .then((res) => {
          this.userList = res.data.data;
        })
        .catch((error) => {});
    },

    selConditionLandlord() {
      this.$axios
        .get("/selConditionLandlord", {
          params: {
            keyWord: this.keyWord,
            currentPage: this.currentPage,
            pageSize: this.pageSize,
          },
        })
        .then((res) => {
          this.userList = res.data.data;
        })
        .catch((srror) => {});
    },

    delAll() {
      this.$axios
        .post("/delUser", this.ids)
        .then((res) => {
          this.localData();
          alert(res.data.messages);
        })
        .catch((res) => {});
    },

    recoverUser() {
      this.$axios
        .post("/recoverUser", this.ids)
        .then((res) => {
          this.localData();
          alert(res.data.messages);
        })
        .catch((res) => {});
    },

    recoverOneUser(id) {
      this.$axios
        .get("/recoverOneUser/" + id)
        .then((res) => {
          this.localData();
          alert(res.data.messages);
        })
        .catch((res) => {});
    },

    delOneUser(id) {
      this.$axios
        .get("/delOneUser/" + id)
        .then((res) => {
          this.localData();
          alert(res.data.messages);
        })
        .catch((res) => {});
    },

    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      if (this.keyWord == "") {
        this.localData();
      } else {
        this.selConditionLandlord();
      }
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      if (this.keyWord == "") {
        this.localData();
      } else {
        this.selConditionLandlord();
      }
    },
    handleSelectionChange(val) {
      var ids = []; //存放选中的id
      for (let item of val) {
        ids.push(item.user_id);
      }
      //将选中的所有项的id放到数据模型中
      this.ids = ids;

      console.log(ids);
      //this.multipleSelection = val;
    },
  },

  mounted() {
    // roleId=sessionStorage.getItem('roleId');
    this.localData();
  },
};
</script>